package io.nanfeng.scope

import java.util.logging.Logger
import kotlin.reflect.KFunction1

interface LoggingContext {
// 1
    val log: Logger // This context provides a reference to a logger
}

// 2
context(LoggingContext)
fun startBusinessOperation() {
    // You can access the log property since LoggingContext is an implicit receiver
    log.info("Operation has started")
}

// 3
fun LoggingContext.test1() {
    // You need to have LoggingContext in a scope as an implicit receiver
    // to call startBusinessOperation()
    startBusinessOperation()
}

// 4
fun test3() {
    // You need to have LoggingContext in a scope as an implicit receiver
    // to call startBusinessOperation()
//    startBusinessOperation()
}

// 5
fun test(loggingContext: LoggingContext) {
    loggingContext.run {
        startBusinessOperation()
    }
    with(loggingContext) {
        // You need to have LoggingContext in a scope as an implicit receiver
        // to call startBusinessOperation()
        startBusinessOperation()
    }
    loggingContext.test1()
}


fun main() {
// 6
    val context = object : LoggingContext {
        override val log: Logger = Logger.getLogger("test")
    }
    test(context)
//    7
    val contextKFunction: KFunction1<LoggingContext, Unit> = ::test
//    8
    contextKFunction(context)
//    9
    val contextFun1: context(LoggingContext) () -> Unit = ::test
    contextFun1(context)
}